package com.company.sendemo.basics.xxFunction.screen;

import org.apache.cxf.helpers.IOUtils;
import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class test {
    public static void main(String[] args) {

        //实例化窗口

        JFrame jf = new JFrame("eagle天堂屏幕监控");
        //设置窗口大小
        jf.setSize(600, 600);
        //显示窗口
        jf.setVisible(true);
        //窗口始终显示在其他窗口的上面
        jf.setAlwaysOnTop(true);
        //窗口关闭的同时程序关闭
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         //开始监控屏幕
         //拿到工具类
        Toolkit tk = Toolkit.getDefaultToolkit();

         //获取屏幕大小
        Dimension dm = tk.getScreenSize();

        //利用label显示图片
        JLabel imageLabel = new JLabel();

        jf.add(imageLabel);

        //利用robot
        try {
            Robot robot = new Robot();
            while (true) {
                 //创建一个矩形
                Rectangle rec = new Rectangle(jf.getWidth(), 0, (int) dm.getWidth() - jf.getWidth(), (int) dm.getHeight());

                //创建一个包含从屏幕中读取的图像
                BufferedImage
                bufImg = robot.createScreenCapture(rec);

                 //将图片设置到label中
                imageLabel.setIcon(new ImageIcon(bufImg));

//                for (int i = 0; i < ; i++) {
//                    http://cn.voidcc.com/question/p-bvuwshqw-bgm.html
//                }
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
                ImageIO.write(bufImg, "jpg", imOut);
                InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
                int a = 2;
                a = a ++;
                OutputStream outStream = new FileOutputStream("D:/study/demo/log/captcha"+a+".jpg");
                IOUtils.copy(inputStream, outStream);

                 //延迟100毫秒
                Thread.sleep(1000);



                        inputStream.close();
                        outStream.close();



            }
        } catch (Exception e) {
            e.printStackTrace();

        }

    }
}
